package hu.kisprof.quiz.view.dialogs;

import hu.kisprof.quiz.view.Labels;
import hu.kisprof.quiz.view.QuizGUI;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class QuizSelectServerDialog extends JDialog implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -383157021091753652L;
	JButton okButton;
	JButton cancelButton;
	JTextField serverF;
	QuizGUI gui;
	

	public QuizSelectServerDialog(QuizGUI gui){
		this.gui=gui;
		this.setLayout(new BorderLayout());
		this.okButton = new JButton(Labels.ok);
		this.cancelButton = new JButton (Labels.cancel);
		this.add(createButtonsPanel(), BorderLayout.SOUTH);
		this.add(createSettingsPanel());
		pack();
		setLocationRelativeTo(gui.getWindow());
		setVisible(true);
	}
	
	private Component createButtonsPanel(){
		JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttons.add(okButton);
		okButton.addActionListener(this);
		buttons.add(cancelButton);
		cancelButton.addActionListener(this);
		
		return buttons;
	}
	
	private Component createSettingsPanel() {
		serverF = new JTextField();
		serverF.setText("localhost");
		return serverF;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals(Labels.ok)){
			if(gui.getControl().setServer(serverF.getText())){
				setVisible(false);
				
			}else{
				JOptionPane.showMessageDialog(this,Labels.serverTimeout,Labels.error, JOptionPane.ERROR_MESSAGE);
			}
			
		}
		if(e.getActionCommand().equals(Labels.cancel)){
			setVisible(false);
		
		}
	}
}